package com.hkts.naturalgas.base.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.naturalgas.base.entity.RoastingonePyjone;
import com.hkts.naturalgas.base.entity.RoastingoneRsoneRsjone;
import com.hkts.naturalgas.base.mapper.RoastingonePyjoneMapper;
import com.hkts.naturalgas.base.service.IRoastingonePyjoneService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.utils.CurveUtils;
import com.hkts.naturalgas.utils.DataHandlerUtils;
import com.hkts.naturalgas.utils.SnowFlakeUtil;
import com.hkts.naturalgas.utils.resultMapUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-06-21
 */
@Service
@Transactional
public class RoastingonePyjoneServiceImpl extends ServiceImpl<RoastingonePyjoneMapper, RoastingonePyjone> implements IRoastingonePyjoneService {


    @Autowired
    private RoastingonePyjoneMapper roastingonePyjoneMapper;

    @Override
    public IPage<RoastingonePyjone> selectPage(Page<RoastingonePyjone> page, RoastingonePyjone roastingonePyjone){
            QueryWrapper<RoastingonePyjone> query=Wrappers.query();
            QueryWrapper<RoastingonePyjone> entityWrapper=getEntityWrapper(query, roastingonePyjone);
            return roastingonePyjoneMapper.selectPage(page,entityWrapper);
    }


    @Override
    public List<RoastingonePyjone> selectList(RoastingonePyjone roastingonePyjone){
            QueryWrapper<RoastingonePyjone> query=Wrappers.query();
            QueryWrapper<RoastingonePyjone> entityWrapper=getEntityWrapper(query, roastingonePyjone);
            return roastingonePyjoneMapper.selectList(entityWrapper);
    }

    /**
     *  公共查询条件
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<RoastingonePyjone> getEntityWrapper(QueryWrapper<RoastingonePyjone> entityWrapper,RoastingonePyjone roastingonePyjone){
            //条件拼接
            return entityWrapper;
    }
    @Override
    public IPage<RoastingonePyjone> selectPageByTime(Page<RoastingonePyjone> page, String startDate, String endDate){
        QueryWrapper<RoastingonePyjone> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= '" + startDate + "'")
                .apply(!"".equals(endDate),
                        "time <= '" + endDate + "'")
                .orderByDesc(RoastingonePyjone::getTime);
        IPage<RoastingonePyjone> hktsTailGasPage = roastingonePyjoneMapper.selectPage(page, query);
        return hktsTailGasPage;
    }
    @Override
    public IPage<RoastingonePyjone> getBrokenLineGraph(Page<RoastingonePyjone> page,String startDate, String endDate){
        QueryWrapper<RoastingonePyjone> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= ' " + startDate + " ' " )
                .apply(!"".equals(endDate),
                        "time <= ' " + endDate + " ' ")
                .orderByAsc(RoastingonePyjone::getTime);
        IPage roastingoneRsoneRsjonePage = roastingonePyjoneMapper.selectPage(page,query);
        List<RoastingonePyjone> records = roastingoneRsoneRsjonePage.getRecords();
        List<Map<String,Object>> resultDate = records.stream().map(t -> assemblyCargoData(t)).collect(Collectors.toList());
        Map<String, List<Object>> stringListMap = resultMapUtils.resultData(resultDate);
        List<Object> list1 = resultMapUtils.getList(resultDate);
        List<Object> listName = resultMapUtils.getListName(resultDate);
        Map<String,Object> data = new HashMap<>();
        data.put("list",listName);
        data.put("records",stringListMap);
        data.put("times",list1);
        List<Object> resultList = new ArrayList<>();
        resultList.add(data);
        IPage iPage = roastingoneRsoneRsjonePage.setRecords(resultList);
        return iPage;
    }

    public Map<String,Object> assemblyCargoData(RoastingonePyjone vo){
        Map<String,Object> data = new LinkedHashMap<>();
        data.put("火道温度1(℃)",vo.getBs1pyj1Hdw1());
        data.put("火道温度2(℃)",vo.getBs1pyj1Hdw2());
        data.put("火道温度3(℃)",vo.getBs1pyj1Hdw3());
        data.put("火道温度4(℃)",vo.getBs1pyj1Hdw4());
        data.put("火道温度5(℃)",vo.getBs1pyj1Hdw5());
        data.put("火道温度6(℃)",vo.getBs1pyj1Hdw6());
        data.put("火道温度7(℃)",vo.getBs1pyj1Hdw7());
        data.put("火道温度8(℃)",vo.getBs1pyj1Hdw8());
        data.put("火道温度9(℃)",vo.getBs1pyj1Hdw9());
        data.put("火道温度10(℃)",vo.getBs1pyj1Hdw10());
        data.put("火道温度11(℃)",vo.getBs1pyj1Hdw11());
        data.put("火道压力1",vo.getBs1pyj1Hdy1());
        data.put("火道压力2",vo.getBs1pyj1Hdy2());
        data.put("火道压力3",vo.getBs1pyj1Hdy3());
        data.put("火道压力4",vo.getBs1pyj1Hdy4());
        data.put("火道压力5",vo.getBs1pyj1Hdy5());
        data.put("火道压力6",vo.getBs1pyj1Hdy6());
        data.put("火道压力7",vo.getBs1pyj1Hdy7());
        data.put("火道压力8",vo.getBs1pyj1Hdy8());
        data.put("火道压力9",vo.getBs1pyj1Hdy9());
        data.put("火道压力10",vo.getBs1pyj1Hdy10());
        data.put("火道压力11",vo.getBs1pyj1Hdy11());
        data.put("time",vo.getTime());
        return data;
    }
    /** 功能描述:
    *  曲线数据
    * @return:
    * @Author: 杨春科
    * @Date: 2021-06-26 15:35
    * @Description //TODO
    */
    @Override
    public void addHostoryData(String startDate, String endDate) throws ParseException {
        CurveUtils utils=new CurveUtils();
        utils.setStartDate(startDate);
        utils.getGrowthRate().add(9.0625f);
        utils.getTimeSlot().add(32);
        utils.setEndDate(endDate);
        utils.getData(260f);
        for(int i=0;i<utils.getDateList().size();i++){
            RoastingonePyjone one=new RoastingonePyjone();
            one.setId(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()));
            one.setTime(utils.getDateList().get(i));
            one.setBs1pyj1Hdw1(DataHandlerUtils.getData(utils.getDataResult().get(i)));
            one.setBs1pyj1Hdw2(DataHandlerUtils.getData(utils.getDataResult().get(i)));
            one.setBs1pyj1Hdw3(DataHandlerUtils.getData(utils.getDataResult().get(i)));
            one.setBs1pyj1Hdw4(DataHandlerUtils.getData(utils.getDataResult().get(i)));
            one.setBs1pyj1Hdw5(DataHandlerUtils.getData(utils.getDataResult().get(i)));
            one.setBs1pyj1Hdw6(DataHandlerUtils.getData(utils.getDataResult().get(i)));
            one.setBs1pyj1Hdw7(DataHandlerUtils.getData(utils.getDataResult().get(i)));
            one.setBs1pyj1Hdw8(DataHandlerUtils.getData(utils.getDataResult().get(i)));
            one.setBs1pyj1Hdw9(DataHandlerUtils.getData(utils.getDataResult().get(i)));
            one.setBs1pyj1Hdw11(DataHandlerUtils.getData(utils.getDataResult().get(i)));
            one.setBs1pyj1Hdw10(DataHandlerUtils.getData(utils.getDataResult().get(i)));
            one.setBs1pyj1Hdy1(0f);
            one.setBs1pyj1Hdy2(0f);
            one.setBs1pyj1Hdy3(0f);one.setBs1pyj1Hdy4(0f);one.setBs1pyj1Hdy5(0f);
            one.setBs1pyj1Hdy6(0f);
            one.setBs1pyj1Hdy7(0f);
            one.setBs1pyj1Hdy8(0f);
            one.setBs1pyj1Hdy9(0f);
            one.setBs1pyj1Hdy10(0f);
            one.setBs1pyj1Hdy11(0f);
            roastingonePyjoneMapper.insert(one);
        }
    }

    @Override
    public void handlerPresserData(String startDate, String endDate) {
        QueryWrapper<RoastingonePyjone> query=Wrappers.query();
        query.le("time",endDate);
        query.isNotNull("bs1pyj1_hdw1");
        List<RoastingonePyjone> roastingonePyjones = roastingonePyjoneMapper.selectList(query);
        for(int i=0;i<roastingonePyjones.size();i++){
            roastingonePyjones.get(i).setBs1pyj1Hdy1(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setBs1pyj1Hdy2(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setBs1pyj1Hdy3(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setBs1pyj1Hdy4(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setBs1pyj1Hdy5(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setBs1pyj1Hdy6(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setBs1pyj1Hdy7(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setBs1pyj1Hdy8(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setBs1pyj1Hdy9(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setBs1pyj1Hdy10(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setBs1pyj1Hdy11(DataHandlerUtils.getPressureRandomNumber());
        }
        this.saveOrUpdateBatch(roastingonePyjones);
    }
    @Override
    public void handlerCurveData(String startDate, String endDate, Float mbt, String order) {
        QueryWrapper<RoastingonePyjone> query=Wrappers.query();
        query.le("time",endDate);
        query.ge("time",startDate);
        List<RoastingonePyjone> roastingonePyjones = roastingonePyjoneMapper.selectList(query);
        List<Float> list=new ArrayList<Float>();
        if(order.equals("desc")){
            list=  DataHandlerUtils.getTemperatureCurve(mbt,roastingonePyjones.get(0).getBs1pyj1Hdw1(),roastingonePyjones.size(),order);
        }else{
            list=  DataHandlerUtils.getTemperatureCurve(mbt,roastingonePyjones.get(roastingonePyjones.size()-1).getBs1pyj1Hdw1(),roastingonePyjones.size(),order);
        }
        for(int i=0;i<roastingonePyjones.size();i++){
            roastingonePyjones.get(i).setBs1pyj1Hdw1( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setBs1pyj1Hdw2(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setBs1pyj1Hdw3( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setBs1pyj1Hdw4(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setBs1pyj1Hdw5( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setBs1pyj1Hdw6(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setBs1pyj1Hdw7( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setBs1pyj1Hdw8(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setBs1pyj1Hdw9( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setBs1pyj1Hdw10(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setBs1pyj1Hdw11( DataHandlerUtils.getData(list.get(i)));
        }
        this.saveOrUpdateBatch(roastingonePyjones);
    }
}
